﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL.Interface.Products;
using DTOS.Products;
using System.Transactions;
using BLL.Concrete.Products;
using BLL.Abstract;
namespace BLL.Products
{
    public class ProductBAL : abResponseObject
    {

        public Int64 CreateProduct(ProductDTO objProduct, IList<StonesDTO> objStones, IList<ImagesDTO> objImages)
        {
            Int64 productId = 0;
            bool result = false;

            try
            {
                using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    if (objProduct != null)
                    {
                        productId = Create(objProduct);
                        if (productId == 0)
                        {
                            ts.Dispose();
                            ResultMessages.Add("Error while creating product details \r\n");

                        }
                        else if (productId > 0)
                        {
                            Result = true;
                            ResultValue = productId;
                            ResultMessages.Add("The product has been added sucessfully \r\n");
                        }
                    }

                    if (objStones != null)
                    {
                        StonesBAL objStonesBAL = new StonesBAL();
                        result = objStonesBAL.Create(objStones);
                        if (!result)
                        {
                            ts.Dispose();
                            productId = 0;
                            ResultMessages.Add("Error while creating stone details \r\n");
                        }

                    }
                    else
                    {
                        ResultMessages.Add("No stone details found \r\n");
                    }
                    if (objImages != null)
                    {
                        ImagesBAL objImagesBAL = new ImagesBAL();
                        result = objImagesBAL.Create(objImages);
                        if (!result)
                        {
                            ts.Dispose();
                            productId = 0;
                            ResultMessages.Add("Error while creating image details \r\n");

                        }
                    }
                    else
                    {
                        ResultMessages.Add("No image details found \r\n");
                    }


                    ts.Complete();
                }

                return productId;
            }

            catch { throw; }
        }

        public Int64 Create(ProductDTO objProduct)
        {
            Int64 productId = 0;
            try
            {
                if (!isExists(objProduct.ProductCode, 0))
                {
                    productId = new DBLayer.Products().CreateProduct(objProduct);

                    if (productId == 0)
                    {
                        throw new Exception("Error while creating product");
                    }

                }
                else
                {
                    throw new Exception("Productcode already exists");
                }

                return productId;
            }

            catch { throw; }


        }

        public bool Update(ProductDTO objProduct)
        {
            bool result = false;
            try
            {
                if (!isExists(objProduct.ProductCode, 0))
                {
                    result = new DBLayer.Products().UpdateProduct(objProduct);

                    if (!result)
                    {
                        throw new Exception("Error while updating product");
                    }

                }
                else
                {
                    throw new Exception("Productcode already exists");
                }

                return result;
            }

            catch { throw; }

        }

        public bool isExists(string productCode, Int64 productId)
        {
            bool isExists = false;
            try
            {
                if (!String.IsNullOrEmpty(productCode))
                {
                    isExists = new DBLayer.Products().isExists(productCode, productId);
                }
                else
                {
                    throw new Exception("Please enter the Productcode");

                }
                return isExists;
            }

            catch { throw; }

        }


    }
}
